﻿namespace Utils
{
    using System;
    using System.Diagnostics;
    using System.Text;
    using System.Web;

    public class Logger
    {
        public static string LogSource = "SPListNavigation.UpButton";

        public static void DebugLocal(object messageObject, string PackageName)
        {
            if (!string.IsNullOrEmpty(PackageName))
                LogSource = PackageName;

            if ((HttpContext.Current == null) || (HttpContext.Current.Request.IsLocal && HttpContext.Current.IsDebuggingEnabled))
            {
                EventLogEntryType failureAudit = EventLogEntryType.FailureAudit;
                if (messageObject != null)
                {
                    try
                    {
                        if (messageObject != null)
                        {
                            string message = messageObject.ToString();
                            if (message.Length > 0x7d00)
                            {
                                message = message.Substring(0x7d00);
                            }
                            try
                            {
                                if (!message.Contains(HttpContext.Current.User.Identity.Name))
                                {
                                    message = HttpContext.Current.User.Identity.Name + " : " + message;
                                }
                            }
                            catch { }

                            using (EventLog log = new EventLog())
                            {
                                log.Source = LogSource + " " + PackageName;
                                log.WriteEntry(message, failureAudit);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception.ToString());
                    }
                }
            }
        }

        public static void Error(object message, string PackageName)
        {
            try
            {
                Message(EventLogEntryType.Error, message, PackageName );

#if DEBUG
                Console.WriteLine(message.ToString());
#endif
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }

        public static void Error(object message, Exception ex, string PackageName)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                if (message != null)
                {
                    builder.Append(message.ToString());
                    builder.Append("\r\n");
                }
                if (ex != null)
                {
                    builder.Append(ex.ToString());
                }
                Message(EventLogEntryType.Error, builder.ToString(), PackageName);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }

        public static void Info(object message, string PackageName)
        {
            try
            {
                Message(EventLogEntryType.Information, message, PackageName);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }

        public static void Message(EventLogEntryType eventLogEntryType, object messageObject, string PackageName)
        {
            if (!string.IsNullOrEmpty(PackageName))
                LogSource = PackageName;

            if (messageObject != null)
            {
                try
                {
                    System.Data.SqlClient.SqlException sqlErr = messageObject as System.Data.SqlClient.SqlException;
                    if (sqlErr != null)
                    {
                        string msgSqlErr;
                        msgSqlErr = "StackTrace:" + sqlErr.StackTrace;
                        using (EventLog log = new EventLog())
                        {
                            log.Source = LogSource;
                            log.WriteEntry(msgSqlErr, eventLogEntryType);
                        }
                    }

                    string message = messageObject.ToString();
                    //if (message.Length > 0x7d00)
                    //{
                    //    message = message.Substring(0x7d00);
                    //}
                    using (EventLog log = new EventLog())
                    {
                        log.Source = LogSource;
                        log.WriteEntry(message, eventLogEntryType);
                    }

                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.ToString());
                }
            }
        }

        public static void Warn(object message, string PackageName)
        {
            try
            {
                Message(EventLogEntryType.Warning, message, PackageName);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
    }
}